﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SF.Utilities;

namespace SF.WebClient.Controllers
{
    public class SelectorController : BasicController
    {
        public static List<dynamic> OrganTreeJson, PositionTreeJson, datagridJson;

        static SelectorController()
        {
            OrganTreeJson = new List<dynamic>();
            OrganTreeJson.Add(new
            {
                id = 1,
                text = "信息中心",
                children = new List<dynamic>() { new { id = 11, text = "研发部" }, new { id = 12, text = "测试部" }, new { id = 13, text = "运维部" } }
            });
            OrganTreeJson.Add(new { id = 2, text = "人资部" });
            OrganTreeJson.Add(new { id = 3, text = "市场部" });

            PositionTreeJson = new List<dynamic>();
            PositionTreeJson.Add(new { id = 1, text = "开发工程师" });
            PositionTreeJson.Add(new { id = 2, text = "运维工程师" });
            PositionTreeJson.Add(new { id = 3, text = "测试工程师" });
            PositionTreeJson.Add(new { id = 4, text = "招聘专员" });
            PositionTreeJson.Add(new { id = 5, text = "培训专员" });
            PositionTreeJson.Add(new { id = 6, text = "营运总监" });
            PositionTreeJson.Add(new { id = 7, text = "技术总监" });
            PositionTreeJson.Add(new { id = 8, text = "IOS开发" });
            PositionTreeJson.Add(new { id = 9, text = "安卓开发" });

            datagridJson = new List<dynamic>();
            datagridJson.Add(new { ID = 1, RealName = "张一", OrganID = 1, PositionID = 1 });
            datagridJson.Add(new { ID = 2, RealName = "张二", OrganID = 1, PositionID = 2 });
            datagridJson.Add(new { ID = 3, RealName = "张三", OrganID = 1, PositionID = 3 });
            datagridJson.Add(new { ID = 4, RealName = "张四", OrganID = 1, PositionID = 4 });
            datagridJson.Add(new { ID = 5, RealName = "张五", OrganID = 1, PositionID = 5 });
            datagridJson.Add(new { ID = 6, RealName = "张六", OrganID = 1, PositionID = 6 });
            datagridJson.Add(new { ID = 7, RealName = "张七", OrganID = 1, PositionID = 7 });
            datagridJson.Add(new { ID = 8, RealName = "张八", OrganID = 1, PositionID = 8 });
            datagridJson.Add(new { ID = 9, RealName = "张九", OrganID = 1, PositionID = 9 });
            datagridJson.Add(new { ID = 10, RealName = "张十", OrganID = 1, PositionID = 1 });

            datagridJson.Add(new { ID = 11, RealName = "李一", OrganID = 2, PositionID = 1 });
            datagridJson.Add(new { ID = 12, RealName = "李二", OrganID = 2, PositionID = 2 });
            datagridJson.Add(new { ID = 13, RealName = "李三", OrganID = 2, PositionID = 3 });
            datagridJson.Add(new { ID = 14, RealName = "李四", OrganID = 2, PositionID = 4 });
            datagridJson.Add(new { ID = 15, RealName = "李五", OrganID = 2, PositionID = 5 });
            datagridJson.Add(new { ID = 16, RealName = "李六", OrganID = 2, PositionID = 6 });
            datagridJson.Add(new { ID = 17, RealName = "李七", OrganID = 2, PositionID = 7 });
            datagridJson.Add(new { ID = 18, RealName = "李八", OrganID = 2, PositionID = 8 });
            datagridJson.Add(new { ID = 19, RealName = "李九", OrganID = 2, PositionID = 9 });
            datagridJson.Add(new { ID = 20, RealName = "李十", OrganID = 2, PositionID = 1 });

            datagridJson.Add(new { ID = 21, RealName = "王一", OrganID = 3, PositionID = 1 });
            datagridJson.Add(new { ID = 22, RealName = "王二", OrganID = 3, PositionID = 2 });
            datagridJson.Add(new { ID = 23, RealName = "王三", OrganID = 3, PositionID = 3 });
            datagridJson.Add(new { ID = 24, RealName = "王四", OrganID = 3, PositionID = 4 });
            datagridJson.Add(new { ID = 25, RealName = "王五", OrganID = 3, PositionID = 5 });
            datagridJson.Add(new { ID = 26, RealName = "王六", OrganID = 3, PositionID = 6 });
            datagridJson.Add(new { ID = 27, RealName = "王七", OrganID = 3, PositionID = 7 });
            datagridJson.Add(new { ID = 28, RealName = "王八", OrganID = 3, PositionID = 8 });
            datagridJson.Add(new { ID = 29, RealName = "王九", OrganID = 3, PositionID = 9 });
            datagridJson.Add(new { ID = 30, RealName = "王十", OrganID = 3, PositionID = 1 });

            datagridJson.Add(new { ID = 31, RealName = "刘一", OrganID = 11, PositionID = 1 });
            datagridJson.Add(new { ID = 32, RealName = "刘二", OrganID = 11, PositionID = 2 });
            datagridJson.Add(new { ID = 33, RealName = "刘三", OrganID = 11, PositionID = 3 });
            datagridJson.Add(new { ID = 34, RealName = "刘四", OrganID = 11, PositionID = 4 });
            datagridJson.Add(new { ID = 35, RealName = "刘五", OrganID = 11, PositionID = 5 });
            datagridJson.Add(new { ID = 36, RealName = "刘六", OrganID = 11, PositionID = 6 });
            datagridJson.Add(new { ID = 37, RealName = "刘七", OrganID = 11, PositionID = 7 });
            datagridJson.Add(new { ID = 38, RealName = "刘八", OrganID = 11, PositionID = 8 });
            datagridJson.Add(new { ID = 39, RealName = "刘九", OrganID = 11, PositionID = 9 });
            datagridJson.Add(new { ID = 40, RealName = "刘十", OrganID = 11, PositionID = 1 });

            datagridJson.Add(new { ID = 41, RealName = "白一", OrganID = 12, PositionID = 1 });
            datagridJson.Add(new { ID = 42, RealName = "白二", OrganID = 12, PositionID = 2 });
            datagridJson.Add(new { ID = 43, RealName = "白三", OrganID = 12, PositionID = 3 });
            datagridJson.Add(new { ID = 44, RealName = "白四", OrganID = 13, PositionID = 4 });
            datagridJson.Add(new { ID = 45, RealName = "白五", OrganID = 13, PositionID = 5 });
            datagridJson.Add(new { ID = 46, RealName = "白六", OrganID = 13, PositionID = 6 });
            datagridJson.Add(new { ID = 47, RealName = "白七", OrganID = 13, PositionID = 7 });
            datagridJson.Add(new { ID = 48, RealName = "白八", OrganID = 13, PositionID = 8 });
            datagridJson.Add(new { ID = 49, RealName = "白九", OrganID = 13, PositionID = 9 });
            datagridJson.Add(new { ID = 50, RealName = "白十", OrganID = 13, PositionID = 1 });

            datagridJson.Add(new { ID = 51, RealName = "黑一", OrganID = 1, PositionID = 1 });
            datagridJson.Add(new { ID = 52, RealName = "黑二", OrganID = 2, PositionID = 2 });
            datagridJson.Add(new { ID = 53, RealName = "黑三", OrganID = 3, PositionID = 3 });
            datagridJson.Add(new { ID = 54, RealName = "黑四", OrganID = 11, PositionID = 4 });
            datagridJson.Add(new { ID = 55, RealName = "黑五", OrganID = 11, PositionID = 5 });
            datagridJson.Add(new { ID = 56, RealName = "黑六", OrganID = 12, PositionID = 6 });
            datagridJson.Add(new { ID = 57, RealName = "黑七", OrganID = 12, PositionID = 7 });
            datagridJson.Add(new { ID = 58, RealName = "黑八", OrganID = 13, PositionID = 8 });
            datagridJson.Add(new { ID = 59, RealName = "黑九", OrganID = 13, PositionID = 9 });
            datagridJson.Add(new { ID = 60, RealName = "黑十", OrganID = 13, PositionID = 1 });
        }

        public void ViewSelectorCssSource()
        {
            this.WriteSourceCode("/Scripts/jquery-easyui-extensions/selector/jeasyui.extensions.selector.css");
        }

        private string BuildViewName(string type, bool full, string data)
        {
            string ret = type;
            if (full) { ret += "Full"; } else { ret += "Sample"; }
            if (data == "local") { ret += ""; } else { ret += "Remote"; }

            return ret;
        }

        #region SingleDatagrid 单datagrid选择框

        public ViewResult SingleDatagridDemo(bool full = false, string data = "local")
        {
            return View(this.BuildViewName("SingleDatagrid", full, data) + "Demo");
        }

        public PartialViewResult SingleDatagridSource()
        {
            return PartialView();
        }

        public void ViewSingleDatagridSource()
        {
            this.WriteSourceCode("/Scripts/jquery-easyui-extensions/selector/jeasyui.extensions.selector.singleDatagrid.js");
        }

        public PartialViewResult SingleDatagridExplain()
        {
            return PartialView();
        }

        #endregion

        #region DblDatagrid 双datagrid选择框

        public ViewResult DblDatagridDemo(bool full = false, string data = "local")
        {
            return View(this.BuildViewName("DblDatagrid", full, data) + "Demo");
        }

        public PartialViewResult DblDatagridSource()
        {
            return PartialView();
        }

        public void ViewDblDatagridSource()
        {
            this.WriteSourceCode("/Scripts/jquery-easyui-extensions/selector/jeasyui.extensions.selector.dblDatagrid.js");
        }

        public PartialViewResult DblDatagridExplain()
        {
            return PartialView();
        }

        #endregion

        #region tree tree选择框

        public ViewResult TreeDemo()
        {
            return View();
        }

        public PartialViewResult TreeSource()
        {
            return PartialView();
        }

        public void ViewTreeSource()
        {
            this.WriteSourceCode("/Scripts/jquery-easyui-extensions/selector/jeasyui.extensions.selector.tree.js");
        }

        public PartialViewResult TreeExplain()
        {
            return PartialView();
        }

        #endregion

        #region treeDblDatagrid 树和双datagrid选择框

        public ViewResult TreeDblDatagridDemo(bool full = false, string data = "local")
        {
            return View(this.BuildViewName("TreeDblDatagrid", full, data) + "Demo");
        }

        public PartialViewResult TreeDblDatagridSource()
        {
            return PartialView();
        }

        public void ViewTreeDblDatagridSource()
        {
            this.WriteSourceCode("/Scripts/jquery-easyui-extensions/selector/jeasyui.extensions.selector.treeDblDatagrid.js");
        }

        public PartialViewResult TreeDblDatagridExplain()
        {
            return PartialView();
        }

        #endregion

        #region accordionDblDatagrid 手风琴和双datagrid选择框

        public ViewResult AccordionDblDatagridDemo(bool full = false, string data = "local")
        {
            return View(this.BuildViewName("AccordionDblDatagrid", full, data) + "Demo");
        }

        public PartialViewResult AccordionDblDatagridSource()
        {
            return PartialView();
        }

        public void ViewAccordionDblDatagridSource()
        {
            this.WriteSourceCode("/Scripts/jquery-easyui-extensions/selector/jeasyui.extensions.selector.accordionDblDatagrid.js");
        }

        public PartialViewResult AccordionDblDatagridExplain()
        {
            return PartialView();
        }

        #endregion

        public JsonResult GetTreeJson()
        {
            return Json(OrganTreeJson);
        }

        public JsonResult GetTreeJson2()
        {
            return Json(PositionTreeJson);
        }

        public JsonResult GetDataGridJson(int? organId, int? positionId, string realName, int page = 1, int rows = 10)
        {
            var data = datagridJson.AsEnumerable();
            if (organId.HasValue)
            {
                data = data.Where(w => w.OrganID == organId);
            }
            if (positionId.HasValue)
            {
                data = data.Where(w => w.PositionID == positionId);
            }
            if (!string.IsNullOrWhiteSpace(realName))
            {
                data = data.Where(w => w.RealName.Contains(realName));
            }

            return Json(new { total = data.Count(), rows = data.Skip((page - 1) * rows).Take(rows) });
        }

        public JsonResult GetSelectedDataGridJson(string selected)
        {
            var ids = SF.Utilities.StringExtensions.ToArray(selected, ",", true).ToInt();

            return Json(datagridJson.Where(w => ids.Contains((int)w.ID)));
        }
    }
}